/* eslint-disable keyword-spacing,indent */
// +----------------------------------------------------------------------
// | CmsWing [ 网站内容管理框架 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2115 http://www.cmswing.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: arterli <arterli@qq.com>
// +----------------------------------------------------------------------

module.exports = class extends think.cmswing.admin {
    constructor(ctx) {
        super(ctx); // 调用父级的 constructor 方法，并把 ctx 传递进去
        // 其他额外的操作
        this.tactive = 'material';
        this.db = this.model('material');
    }
    /**
     * index action
     * @return {Promise} []
     */
    async indexAction() {
        const query = unflatten(this.get());
        delete query.page;
        const dataSelect = await this.db.where({ level: 1, status: 1 }).order('sort asc').select();
        const dataMap = {};
        dataSelect.forEach(e => {
            dataMap[e.id] = e.label;
        });
        const data = await this.db.where(query).page(this.get('page') || 1, 20).order('id asc').countSelect();
        const html = this.pagination(data);
        this.assign('dataMap', dataMap);
        this.assign('dataSelect', dataSelect);
        this.assign('pagerData', html); // 分页展示使用
        this.assign('list', data.data);
        this.meta_title = '商品维护';
        return this.display();
    }

    async indexexportAction() {
        const query = unflatten(this.get());
        delete query.page;
        if(!query.pid) {
            delete query.pid;
        }
        const data = await this.db.where(query).order('id asc').select();
        const pathname = get_pathname(this.referer());
        const csvdata = await json2csv(_csvconfig[pathname], data);
        this.ctx.set('Content-Type', 'application/vnd.openxmlformats;charset=utf-8');
        this.ctx.set('Content-Disposition', 'attachment; filename=' + encodeURI(get_remark_type(pathname)) + '.csv');
        this.ctx.body = csvdata;
    }

    async addAction() {
        if (this.isPost) {
            const postData = this.post();
            postData.create_time = Date.now();
            if (postData.pid) {
                postData.level = 2;
            } else {
                postData.pid = 0;
                postData.level = 1;
            }
            const check = await this.db.where({ pid: postData.pid, label: postData.label }).find();
            if (!think.isEmpty(check)) {
                return this.fail('物料重复！');
            }
            const res = await this.db.add(postData);
            if (res) {
                return this.success({
                    name: '操作成功！',
                    url: this.referer()
                });
            } else {
                return this.fail('操作失败！');
            }
        } else {
            const dataSelect = await this.db.where({ level: 1, status: 1 }).order('sort asc').select();
            const dataMap = {};
            dataSelect.forEach(e => {
                dataMap[e.id] = e.label;
            });
            this.assign('dataSelect', dataSelect);
            this.meta_title = '新增物料';
            return this.display();
        }
    }
    async activeAction() {
        const ids = this.para('ids');
        const status = this.para('status');
        if(!ids) {
            return this.fail('请选择要操作的物料');
        }
        const updateC = await this.db.where({ id: ['in', ids] }).update({status: status});
        if (updateC) {
            return this.success({
                name: '操作成功！',
                url: this.referer()
            });
        } else {
            return this.fail('操作失败！');
        }
    }
};
